package controller;

import Pojo.Apply;
import Pojo.Houselist;
import Pojo.User;
import Pojo.Userlist;
import Pojo.Zulist;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.ApplyService;
import service.HouselistService;
import service.UserlistService;

@Controller
public class ApplyController
{

  @Autowired
  private UserlistService userlistService;

  @Autowired
  private HouselistService houselistService;

  @Autowired
  private ApplyService applyService;

  @RequestMapping({"/applycheckuserlist"})
  public String applycheckuserlist(HttpSession httpSession, Model model, Integer id)
  {
    User user1 = (User)httpSession.getAttribute("user");
    Integer user_id = user1.getId();
    Userlist list = this.userlistService.findhasuserlist(user_id);
    if (list == null) {
      model.addAttribute("error", "applycheck");
      return "redirect:houselist.action";
    }
    Houselist houselist = this.houselistService.findid(id.intValue());
    houselist.setStatus("已被申请");
    this.houselistService.updatehousestatus(houselist);
    Integer userlist_id = list.getId();
    Apply apply = new Apply();
    apply.setHouse_id(houselist.getHouseid());
    apply.setAddress(houselist.getAddress());
    apply.setPrice(houselist.getPrice());
    apply.setArea(houselist.getArea());
    apply.setStatus("申请中");
    apply.setUserlist_id(userlist_id);
    this.applyService.insertapply(apply);
    model.addAttribute("error", "applysuccess");
    return "redirect:houselist.action";
  }

  @RequestMapping({"/findapplylist"})
  public String findapplylist(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
    throws Exception
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List applylist = this.applyService.findapplylist();
    PageInfo p = new PageInfo(applylist);
    model.addAttribute("applylist", applylist);
    model.addAttribute("p", p);
    model.addAttribute("mainPage", "applylist.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/applychangehousestatus"})
  public String applychangehousestatus(HttpSession httpSession, Model model, String house_id) throws Exception {
    User user1 = (User)httpSession.getAttribute("user");
    Integer user_id = user1.getId();
    Userlist userlist = this.userlistService.findhasuserlist(user_id);
    Houselist houselist = this.houselistService.findhouseid(house_id);
    houselist.setStatus("已租赁");
    this.houselistService.updatehousestatus(houselist);
    Zulist zulist = new Zulist();
    zulist.setHouse_id(house_id);
    zulist.setPrice(Double.valueOf(houselist.getPrice()));
    zulist.setAddress(houselist.getAddress());

    return "";
  }

  @RequestMapping({"/refuseapply"})
  public String refuseapply(String house_id, Model model) {
    Houselist houselist = new Houselist();
    houselist.setHouseid(house_id);
    houselist.setStatus("未租赁");
    this.applyService.refuseapply(houselist);

    return "redirect:findapplylist.action";
  }

  @RequestMapping({"/getmyapply"})
  public String getmyapply(Model model, HttpSession httpSession, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    User user1 = (User)httpSession.getAttribute("user");
    Userlist userlist = this.userlistService.findhasuserlist(user1.getId());
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List list = this.userlistService.getmyapply(userlist.getId());
    PageInfo p = new PageInfo(list);
    model.addAttribute("userlist", list);
    model.addAttribute("p", p);
    model.addAttribute("mainPage", "myapply.jsp");
    return "zuke/main";
  }
}